<%@ include file="../import_page.jsp"%>
<%@ page import="org.json.*"%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.admin)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;

	ArrayList<Users> listUser = null;
	try {
		UnitsControl unitCon = new UnitsControl();
		listUser = unitCon.getUsersFromUnit(selectUnit.getId());
		unitCon.closeDBConnection();
	} catch (Exception err) {
		errMsg = err.toString();
	}
	
	JSONArray jArr = new JSONArray();
	if(listUser != null) {
		Iterator<Users> luI = listUser.iterator();
		while(luI.hasNext()) {
			Users aUser = luI.next();	
			JSONObject jObj = new JSONObject();
			jObj.put("id", aUser.getId());
			jObj.put("name", aUser.getName());
			jArr.put(jObj);
		}
	} 
%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<script type="text/javascript" src="<%=URL_JQUERY %>"></script>
<script type="text/javascript" src="../js/jquery.json-2.2.js"></script>
<script type="text/javascript">
var inUnitUsers = <%=jArr.toString() %>;
var selectUnit = "<%=selectUnit.getId() %>";
</script>
<script type="text/javascript" src="../js/f_modify_users_unit.js"></script>
<style type="text/css">
<!--
td.mainCol {
	width: 45%;
}

select.mainDdl {
	width: 100%;
}

.err {
	color: red;
}
-->
</style>
</head>

<body>
	<%if(!isEmptyString(errMsg)) {%>
	<p><%=errMsg %></p>
	<%} %>
	<p>
		Unit:
		<%=selectUnit.getName() %>
		(<%= selectUnit.getId() %>)
	</p>
	<hr />
	<table border="1">
		<tr>
			<td class="mainCol">In Unit</td>
			<td>&nbsp;</td>
			<td class="mainCol">
				<form id="search_user_form">
					<table width="100%" border="1">
						<tr>
							<td><input type="text" name="txtKey" id="txtKey" /><input
								type="submit" id="btnSearch" value="Search" />
							</td>
						</tr>
						<tr>
							<td>Type: <input type="radio" name="rdUserType"
								id="rdStudent" checked="checked" />Student<input type="radio"
								name="rdUserType" id="rdStaff" />Staff</td>
						</tr>
					</table>
				</form></td>
		</tr>
		<tr>
			<td><select multiple="multiple" size="20" id="ddlInUnit"
				class="mainDdl">
			</select></td>
			<td>
				<button type="button" id="btnAddAll">&lt;&lt;</button>
				<br />
				<button type="button" id="btnAddSelect">&lt;</button>
				<br /> <br />
				<button type="button" id="btnDelSelect">&gt;</button>
				<br />
				<button type="button" id="btnDelAll">&gt;&gt;</button>
				<br /></td>
			<td><select multiple="multiple" size="20" id="ddlOther"
				class="mainDdl">
			</select></td>
		</tr>
		<tr>
			<td colspan="3">
				<button type="button" id="btnUndoAll">Undo All Change</button>
				<button type="button" id="btnSubmit">Submit Change</button></td>
		</tr>
		<tr>
			<td colspan="3" id="submitResult"></td>
		</tr>
	</table>
</body>
</html>